<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <form action="/upload" method="POST" enctype="multipart/form-data">
    <input type="file" name="img" id="file">
    <img width="100" src="" id="img" alt="">
    <button type="submit">上传</button>
  </form>
  <script>
    var file = document.getElementById('file');
    var img = document.getElementById('img');
    file.onchange = function(){
      // 获取 选中的文件  files
      console.log(this.files[0]);
      var fd = new FileReader();  
      fd.readAsDataURL(this.files[0]);
      fd.onload = function(){
        /* 
        文件读取完成时触发
        fd.result
        */
        img.src = this.result;
      }
    }

    /* 
    FileReader
    功能 将 二进制数据流 读取成 相关格式
    */
  </script>
</body>
</html>